﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Reflection;

namespace T3K.SketchEditor.AvrBuilder {
    class Program {

        static void Main(string[] args) {

            FileInfo fInfo = new FileInfo(Assembly.GetEntryAssembly().Location);
            ArgsParser parser = new ArgsParser(fInfo.Name);
            StringBuilder sBuilderDesc = new StringBuilder();

            sBuilderDesc.AppendFormat("{0} is a compile wrapper for the winavr gcc tool chain\n", fInfo.Name);
            sBuilderDesc.AppendLine("it's used by the sketch editor to compile avr sketches pde or cpp based");
            sBuilderDesc.AppendLine("Variables can be included in the default avrbuilder.vars file.");
            sBuilderDesc.AppendLine("Other variables files can be included in de build process with the");
            sBuilderDesc.AppendLine("'i' param.");

            parser.Description = sBuilderDesc.ToString();

            parser["v"] = new ArgsParser.Argument("v", ArgsParser.ArgumentType.Argument, false);
            parser["i"] = new ArgsParser.Argument("i", ArgsParser.ArgumentType.Argument, false);
            parser["c"] = new ArgsParser.Argument("c", ArgsParser.ArgumentType.Argument, false);
            parser["t"] = new ArgsParser.Argument("t", ArgsParser.ArgumentType.Argument, false);
            parser["p"] = new ArgsParser.Argument("p", ArgsParser.ArgumentType.Switch, false);
            parser["h"] = new ArgsParser.Argument("h", ArgsParser.ArgumentType.Switch, false);

            parser["v"].Parameter = "level";
            parser["i"].Parameter = "filename";
            parser["c"].Parameter = "command";
            parser["t"].Parameter = "target";

            parser["v"].Description = "Verbose Level 0 to 4 (Default: 0)";
            parser["i"].Description = "File with variables to include";
            parser["c"].Description = "Command to Execute (Default: ALL)";
            parser["t"].Description = "Target (i.e. $(TARGET).elf)";
            parser["p"].Description = "will end with 'press enter to continue'";
            parser["h"].Description = "Displays this help screen";

            try {

                Builder builder = new Builder();
                builder.Verbose = 0;

                parser.Parse(args);

                if (parser["h"].Match) {
                    Console.WriteLine(parser.Usage());
                    return;
                }

                string defaultVarsFile = "avrbuilder.vars";
                if (File.Exists(defaultVarsFile))
                    builder.Include(defaultVarsFile);

                if (parser["i"].Match) {
                    List<string> files = parser["i"].Value;
                    for (int i = 0; i < files.Count; i++) {
                        builder.Include(files[i]);
                    }
                }

                if (parser["v"].Match) {
                    int verboseLevel;
                    if (int.TryParse(parser["v"].Value[0], out verboseLevel))
                        builder.Verbose = verboseLevel;
                }

                string command = "ALL";

                if (parser["c"].Match)
                    command = parser["c"].Value[0].ToUpper();

                string target = (new DirectoryInfo(".")).Name;

                if (parser["t"].Match)
                    target = parser["t"].Value[0].ToUpper();
                
                builder["TARGET"] = target;

                builder.Make(command);

            } catch (Exception ex) {
                Console.WriteLine("ERROR: {0}", ex.Message);
            } finally {

                if (parser["p"].Match) {
                    Console.WriteLine("Press enter to continue...");
                    Console.ReadLine();
                }
            }
        }
    }
}
